package com.nannybear.View;

import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nannybear.R;

import java.util.Observable;
import java.util.Observer;

/**
 * @brief Home view
 */
public class HomeScreen extends Screen implements Observer{
    /********** DECLARATIONS **********/
    //Variables
    private String newValueTemp;
    private String newValueHum;
    private int newValueBrightness;
    private int newValueState;
    private int valueThresholdTemp;
    private int valueThresholdHum;
    private int NORMAL = 1;
    private int MODERATE = 2;
    private int CRITICAL = 3;

    // Widget of the view
    private ImageView warning_temp ,warning_hum;
	private RelativeLayout home_screen_layout;
    private TextView temp_value, light_value, humidity_value, state_value;

    // Tab buttons
    private ImageButton homeButton;
    private ImageButton tempButton;
    private ImageButton brightnessButton;
    private ImageButton humidityButton;
    private ImageButton babyStateButton;
    private ImageButton settingsButton;
    private ImageButton helpButton;

    // Handler
    final Handler myHandler = new Handler();

    /************ METHODS ************/
    /**
     * @brief constructor.
     */
    public HomeScreen(){
        this.valueThresholdTemp = NORMAL;
        this.valueThresholdHum = NORMAL;
    }

    /**
     * @brief Reception of a new value
     */
    public void update(Observable obs, Object obj){
        String name = obs.getClass().getSimpleName();
        if(name.equalsIgnoreCase("Temperature")){
            setNewValueTemp(obj.toString());
            updateTemp();
        }
        if(name.equalsIgnoreCase("Humidity")){
            setNewValueHum(obj.toString());
            updateHumidity();
        }
        if(name.equalsIgnoreCase("Brightness")){
            setNewValueBrightness(Integer.parseInt(obj.toString()));
            updateBrightness();
        }
        if(name.equalsIgnoreCase("BabyState")){
            setNewValueState(Integer.parseInt(obj.toString()));
            updateState();
        }
    }

    /**
     * @brief Post a message in the UI thread for the temperature
     */
    public void updateTemp() {
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                temp_value.setText(getNewValueTemp());
            }
        });
    }

    /**
     * @brief Post a message in the UI thread for the humidity
     */
    public void updateHumidity() {
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                humidity_value.setText(getNewValueHum());
            }
        });
    }

    /**
     * @brief Post a message in the UI thread for the brightness
     */
    public void updateBrightness() {
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                int value = getNewValueBrightness();
                if(value == 1){
                    light_value.setText("dark");
                }
                if(value == 2){
                    light_value.setText("dim light");
                }
                if(value == 3){
                    light_value.setText("bright");
                }
            }
        });
    }

    /**
     * @brief Post a message in the UI thread for the baby state
     */
    public void updateState() {
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                int value = getNewValueState();
                // TODO : aller recuperer l'état actuelle sur babyState pour savoir si agité ou autre
                if(value == 1){
                    state_value.setText(" quiet");
                }
                if(value == 2){
                    state_value.setText(" making noise");
                }
            }
        });
    }

    /**
     * @brief Display the temperature tab with good color
     */
    public void displayTabTemp(){
        int thresholdTemp;
        thresholdTemp = getValueThresholdTemp();
        if (thresholdTemp == NORMAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey);
        }
        if (thresholdTemp == MODERATE) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
            warning_temp.setVisibility(View.VISIBLE);
        }
        if (thresholdTemp == CRITICAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
            warning_temp.setVisibility(View.VISIBLE);
        }
    }

    /**
     * @brief Display the humidity tab with good color
     */
    public void displayTabHum(){
        int thresholdHum;
        thresholdHum = getValueThresholdHum();
        if (thresholdHum == NORMAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        }
        if (thresholdHum == MODERATE) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
            warning_hum.setVisibility(View.VISIBLE);
        }
        if (thresholdHum == CRITICAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
            warning_hum.setVisibility(View.VISIBLE);
        }
    }

    /**
     * @brief Show the view
     */
    public void display(){
        displayTabTemp();
        displayTabHum();
        home_screen_layout.setVisibility(View.VISIBLE);
        homeButton.setBackgroundResource(R.drawable.home_active);
    }

    /**
     * @brief Close the view with the good color of temperature tab
     */
    public void closeTabTemp(){
        int thresholdTemp;
        thresholdTemp = getValueThresholdTemp();
        if (thresholdTemp == NORMAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey);
        }
        if (thresholdTemp == MODERATE) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        }
        if (thresholdTemp == CRITICAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        }
    }

    /**
     * @brief Close the view with the good color of humidity tab
     */
    public void closeTabHum(){
        int thresholdHum;
        thresholdHum = getValueThresholdHum();
        if (thresholdHum == NORMAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        }
        if (thresholdHum == MODERATE) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        }
        if (thresholdHum == CRITICAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        }
    }

    /**
     * @brief Close the view
     */
    public void close(){
        closeTabTemp();
        closeTabHum();
        home_screen_layout.setVisibility(View.GONE);
        warning_temp.setVisibility(View.GONE);
        warning_hum.setVisibility(View.GONE);
        homeButton.setBackgroundResource(R.drawable.home_grey);
    }


    /*********** ACCESSORS ***********/
    public void setHomeButton(ImageButton homeBtn) {
        this.homeButton = homeBtn;
    }

    public ImageButton getHomeButton() {
        return homeButton;
    }

    public void setTempButton(ImageButton tempBtn) {
        this.tempButton = tempBtn;
    }

    public ImageButton getTempButton() {
        return tempButton;
    }

    public void setBrightnessButton(ImageButton brightnessBtn) {
        this.brightnessButton = brightnessBtn;
    }

    public ImageButton getBrightnessButton() {
        return brightnessButton;
    }

    public void setHumidityButton(ImageButton humidityBtn) {
        this.humidityButton = humidityBtn;
    }

    public ImageButton getHumidityButton() {
        return humidityButton;
    }

    public void setBabyStateButton(ImageButton babyStateBtn) {
        this.babyStateButton = babyStateBtn;
    }

    public ImageButton getBabyStateButton() {
        return babyStateButton;
    }

    public void setSettingsButton(ImageButton settingsBtn) {
        this.settingsButton = settingsBtn;
    }

    public ImageButton getSettingsButton() {
        return settingsButton;
    }

    public void setHelpButton(ImageButton helpBtn) {
        this.helpButton = helpBtn;
    }

    public ImageButton getHelpButton() {
        return helpButton;
    }
    
    public ImageView getWarning_temp() {
		return warning_temp;
	}

	public void setWarning_temp(ImageView warning_temp) {
		this.warning_temp = warning_temp;
	}

	public ImageView getWarning_hum() {
		return warning_hum;
	}

	public void setWarning_hum(ImageView warning_hum) {
		this.warning_hum = warning_hum;
	}

	public RelativeLayout getHome_screen_layout() {
		return home_screen_layout;
	}

	public void setHome_screen_layout(RelativeLayout home_screen_layout) {
		this.home_screen_layout = home_screen_layout;
	}

    public String getNewValueTemp() {
        return newValueTemp;
    }

    public void setNewValueTemp(String newValueTemp) {
        this.newValueTemp = newValueTemp;
    }

    public TextView getTemp_value() {
        return temp_value;
    }

    public void setTemp_value(TextView temp_value) {
        this.temp_value = temp_value;
    }

    public TextView getLight_value() {
        return light_value;
    }

    public void setLight_value(TextView light_value) {
        this.light_value = light_value;
    }

    public TextView getHumidity_value() {
        return humidity_value;
    }

    public void setHumidity_value(TextView humidity_value) {
        this.humidity_value = humidity_value;
    }

    public TextView getState_value() {
        return state_value;
    }

    public void setState_value(TextView state_value) {
        this.state_value = state_value;
    }

    public String getNewValueHum() {
        return newValueHum;
    }

    public void setNewValueHum(String newValueHum) {
        this.newValueHum = newValueHum;
    }

    public int getNewValueBrightness() {
        return newValueBrightness;
    }

    public void setNewValueBrightness(int newValueBrightness) {
        this.newValueBrightness = newValueBrightness;
    }

    public int getNewValueState() {
        return newValueState;
    }

    public void setNewValueState(int newValueState) {
        this.newValueState = newValueState;
    }

    public int getValueThresholdTemp() {
        return valueThresholdTemp;
    }

    public void setValueThresholdTemp(int valueThresholdTemp) {
        this.valueThresholdTemp = valueThresholdTemp;
    }

    public int getValueThresholdHum() {
        return valueThresholdHum;
    }

    public void setValueThresholdHum(int valueThresholdHum) {
        this.valueThresholdHum = valueThresholdHum;
    }
}
